<template>
  <div id="updateQuestionView">
    <a-steps changeable :current="current" @change="setCurrent">
      <a-step description="title And tags">基本信息</a-step>
      <a-step description="content">题目内容</a-step>
      <a-step description="answer">题目答案</a-step>
      <a-step description="judgeConfig">判题配置</a-step>
      <a-step description="judgeCase">判题用例</a-step>
    </a-steps>
    <div
      :style="{
        width: '45%',
        height: '200px',
        // textAlign: 'center',
        background: 'var(--color-bg-2)',
        color: '#C2C7CC',
        margin: '5% 25%',
      }"
    >
      <!--      <div style="line-height: 160px">Step{{ current }} Content</div>-->
      <a-form :model="form" label-align="left">
        <div v-if="current === 1">
          <a-form-item field="title" label="标题">
            <a-input v-model="form.title" placeholder="请输入标题" />
          </a-form-item>
          <a-form-item field="tags" label="标签">
            <a-input-tag
              v-model="form.tags"
              placeholder="请选择标签(点击回车添加标签)"
              allow-clear
            />
          </a-form-item>
        </div>
        <div v-if="current === 2">
          <a-form-item field="content">
            <MdEditor :value="form.content" :handle-change="onContentChange" />
          </a-form-item>
        </div>
        <div v-if="current === 3">
          <a-form-item field="answer">
            <MdEditor :value="form.answer" :handle-change="onAnswerChange" />
          </a-form-item>
        </div>
        <div v-if="current === 4">
          <a-form-item :content-flex="false" :merge-props="false">
            <a-space direction="vertical" style="min-width: 480px">
              <a-form-item field="judgeConfig.timeLimit" label="时间限制">
                <a-input-number
                  v-model="form.judgeConfig.timeLimit"
                  placeholder="请输入时间限制"
                  mode="button"
                  min="0"
                  size="large"
                />
              </a-form-item>
              <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
                <a-input-number
                  v-model="form.judgeConfig.memoryLimit"
                  placeholder="请输入内存限制"
                  mode="button"
                  min="0"
                  size="large"
                />
              </a-form-item>
              <a-form-item field="judgeConfig.stackLimit" label="堆栈限制">
                <a-input-number
                  v-model="form.judgeConfig.stackLimit"
                  placeholder="请输入堆栈限制"
                  mode="button"
                  min="0"
                  size="large"
                />
              </a-form-item>
            </a-space>
          </a-form-item>
        </div>
        <div v-if="current === 5">
          <a-form-item :content-flex="false" :merge-props="false">
            <a-form-item
              v-for="(judgeCaseItem, index) of form.judgeCase"
              :key="index"
              no-style
            >
              <a-space direction="vertical" style="min-width: 640px">
                <a-form-item
                  :field="`form.judgeCase[${index}].input`"
                  :label="`输入用例-${index}`"
                  :key="index"
                >
                  <a-input
                    v-model="judgeCaseItem.input"
                    placeholder="请输入测试输入用例"
                  />
                </a-form-item>
                <a-form-item
                  :field="`form.judgeCase[${index}].output`"
                  :label="`输出用例-${index}`"
                  :key="index"
                >
                  <a-input
                    v-model="judgeCaseItem.output"
                    placeholder="请输入测试输出用例"
                  />
                </a-form-item>
                <a-button status="danger" @click="handleDelete(index)">
                  删除
                </a-button>
              </a-space>
            </a-form-item>
            <div style="margin-top: 32px">
              <a-button @click="handleAdd" type="outline" status="success"
                >新增测试用例
              </a-button>
            </div>
            <a-form-item> </a-form-item>
          </a-form-item>
        </div>
      </a-form>
      <div>
        <a-space size="large" style="float: right">
          <a-button type="secondary" :disabled="current <= 1" @click="onPrev">
            <IconLeft />
            上一步
          </a-button>
          <a-button
            v-show="current !== 5"
            type="primary"
            :disabled="current >= 5"
            @click="onNext"
          >
            下一步
            <IconRight />
          </a-button>
          <a-button
            v-show="current === 5"
            type="primary"
            style="min-width: 200px"
            @click="doSubmit"
            >提交
          </a-button>
        </a-space>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";
import MdEditor from "@/components/MdEditor.vue";

const route = useRoute();

let headline = ref({
  headline: true,
});
// 如果页面地址包含 update，视为更新页面
const updatePage = route.path.includes("update");

let form = ref({
  title: "",
  tags: [],
  answer: "",
  content: "",
  judgeConfig: {
    memoryLimit: 1000,
    stackLimit: 1000,
    timeLimit: 1000,
  },
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
});

/**
 * 根据题目 id 获取老的数据
 */
const loadData = async () => {
  const id = route.query.id;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(
    id as any
  );
  if (res.code === 0) {
    form.value = res.data as any;
    // json 转 js 对象
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          input: "",
          output: "",
        },
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as any);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 1000,
        stackLimit: 1000,
        timeLimit: 1000,
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as any);
    }
    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      form.value.tags = JSON.parse(form.value.tags as any);
    }
  } else {
    message.error("加载失败，" + res.message);
  }
};

onMounted(() => {
  loadData();
});

const doSubmit = async () => {
  console.log(form.value);
  // 区分更新还是创建
  if (updatePage) {
    headline.value.headline = false;
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("更新成功");
    } else {
      message.error("更新失败，" + res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("创建成功");
    } else {
      message.error("创建失败，" + res.message);
    }
  }
};

/**
 * 新增判题用例
 */
const handleAdd = () => {
  form.value.judgeCase.push({
    input: "",
    output: "",
  });
};

/**
 * 删除判题用例
 */
const handleDelete = (index: number) => {
  form.value.judgeCase.splice(index, 1);
};

const onContentChange = (value: string) => {
  form.value.content = value;
};

const onAnswerChange = (value: string) => {
  form.value.answer = value;
};

// 定义响应式数据
const current = ref<number>(1);

// 上一步
const onPrev = (): void => {
  current.value = Math.max(1, current.value - 1);
};

// 下一步
const onNext = (): void => {
  current.value = Math.min(5, current.value + 1);
  if (current.value === 5) {
    console.log(current.value);
  }
};

// 设置当前步骤
const setCurrent = (value: number): void => {
  current.value = value;
  if (current.value === 5) {
    console.log(current.value);
  }
};
</script>

<style scoped>
#updateQuestionView {
  height: 100vh;
}
</style>
